package com.myNovel.novel.controller.front;

import com.myNovel.novel.core.auth.UserHolder;
import com.myNovel.novel.core.common.constant.ApiRouterConstants;
import com.myNovel.novel.core.common.resp.RestResp;
import com.myNovel.novel.dao.entity.BookComment;
import com.myNovel.novel.dto.req.UserCommentReqDto;
import com.myNovel.novel.dto.req.UserRegisterReqDto;
import com.myNovel.novel.dto.resp.BookCommentRespDto;
import com.myNovel.novel.dto.resp.UserRegisterRespDto;
import com.myNovel.novel.service.BookService;
import com.myNovel.novel.service.UserService;
import com.myNovel.novel.service.impl.UserServiceImpl;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：Guxx
 * @Package：com.myNovel.novel.controller.front
 * @Date：2024/3/30 16:06
 * @Filename：UserController
 */
@RestController
@RequestMapping(ApiRouterConstants.API_FRONT_HOME_URL_PREFIX)
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final BookService bookService;
    /**
     * 用户注册接口
     */
    @PostMapping("register")
    public RestResp<UserRegisterRespDto> register(@Valid @RequestBody UserRegisterReqDto dto) {
        return userService.register(dto);
    }

    /**
     * 发表评论接口
     * */
    @PostMapping("comment")
    public RestResp<Void> comment(@PathVariable UserCommentReqDto dto){
        dto.setUserId(UserHolder.getUserId());
        return bookService.saveComment(dto);
    }
    //修改评论接口
    @PutMapping("comment/{id}")
    public RestResp<Void> updateComment(@PathVariable Long id, String content){
        return bookService.updateComment(UserHolder.getUserId(),id,content);
    }
    //删除评论接口
    @DeleteMapping("comment/{id}")
    public RestResp<Void> deleteComment(@PathVariable Long id){
        return bookService.deleteComment(UserHolder.getUserId(),id);
    }
    //查询最新评论
    @GetMapping("comment/newest_list")
    public RestResp<BookCommentRespDto> listNewestComments(Long bookId){
        return bookService.listNewestComments(bookId);
    }
}
